unit uMArray;

interface

type
  TMArray = class
    protected
      marray : array of TObject;
      _needToDelete : boolean;
    public

      constructor Create(NeedToDelete:boolean);
      procedure Add(item : Pointer);
      procedure delete(index : integer);
      function GetItem(Index: integer) : Pointer;
      procedure SetItem(Index : integer; const val : Pointer);
      procedure clear;

      function mhigh() : integer;
      property Items[Index: Integer]: Pointer read GetItem write SetItem; default;
      destructor destroy; override;
  end;

implementation
constructor TMArray.Create(NeedToDelete:boolean);
begin
  setLength(marray, 0);
  _needToDelete := NeedToDelete;
end;

function TMArray.GetItem(Index : integer) : Pointer;
begin
  GetItem := marray[Index];
end;

procedure TMArray.SetItem(Index : integer; const val : Pointer);
begin
  marray[Index] := val;
end;

procedure TMArray.Add(item: Pointer);
begin
  setLength(marray, length(marray) + 1);
  marray[high(marray)] := item;
end;

procedure TMArray.delete(index: Integer);
begin
  if(_NeedToDelete) then
    marray[index].Free;
  marray[index] := marray[high(marray)];
  setLength(marray, high(marray));
end;

procedure TMArray.clear;
var
  i: Integer;
begin
  for i := 0 to high(marray) do
    if (Assigned(marray[i])) then begin
      if(_NeedToDelete) then
        marray[i].Free;
     end;
  setLength(marray, 0);
end;

function TMarray.mhigh : integer;
begin
  result := high(marray);
end;

destructor TMarray.destroy;
begin
  clear;
end;

end.
